﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
    public class BranchInfoService
    {
        DBHelper db = new DBHelper();
        //查找所有BranchInfo信息
        public IList<BranchInfo> GetAllBranchInfo()
        {
            try
            {
                DataTable dt = db.getDataTable("proc_getAllBranchInfo");
                List<BranchInfo> branchInfoList = new List<BranchInfo>();

                foreach (DataRow row in dt.Rows)
                {
                    BranchInfo branchInfo = new BranchInfo();
                    branchInfo.BranchId = (int)row["BranchId"];
                    branchInfo.BranchName = (string)row["BranchName"];
                    branchInfo.BranchShortName = (string)row["BranchShortName"];
                    branchInfoList.Add(branchInfo);
                }

                return branchInfoList;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //根据BranchId查找BranchInfo
        public BranchInfo GetBranchInfoByBranchId(int branchId)
        {
            BranchInfo branchInfo = new BranchInfo();
            using (SqlDataReader reader = db.getDataReader("proc_getBranchInfobyBranchId", new SqlParameter("@branchId", branchId)))
            {
                if (reader.Read())
                {
                    branchInfo.BranchId = (int)reader["BranchId"];
                    branchInfo.BranchName = (string)reader["BranchName"];
                    branchInfo.BranchShortName = (string)reader["BranchShortName"];
                  
                }
                reader.Close();
                db.CloseConnection();
                return branchInfo;
            }
        }
        //根据BranchName查找BranchInfo
        public BranchInfo GetBranchInfoByBranchName(string brabchName)
        {
            try
            {
                BranchInfo branchInfo = new BranchInfo();
                using (SqlDataReader reader = db.getDataReader("proc_getBranchInfobyBranchName", new SqlParameter("@BranchName", brabchName)))
                {
                    if (reader.Read())
                    {
                        branchInfo.BranchId = (int)reader["BranchId"];
                        branchInfo.BranchName = (string)reader["BranchName"];
                        branchInfo.BranchShortName = (string)reader["BranchShortName"];
                        reader.Close();
                    }
                    db.CloseConnection();
                    return branchInfo;
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        //添加BranchInfo信息
        public int AddBranchInfo(BranchInfo branchInfo)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]{ 
                                new SqlParameter("@BranchName", branchInfo.BranchName),
                                new SqlParameter("@BranchShortName",branchInfo.BranchShortName) };
                int result = db.getCount("proc_insertBranchInfo", para);
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }


        }
        //修改BranchInfo信息
        public int UpdateBranchInfoByBranchId(BranchInfo branchInfo)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[] {
                    new SqlParameter("@BranchId",branchInfo.BranchId),
                    new SqlParameter("@BranchName", branchInfo.BranchName),
                    new SqlParameter("@BranchShortName",branchInfo.BranchShortName)
                };
                int num = db.getCount("proc_updateBranchInfobyBranchId", para);
                return num;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }
        //删除BranchInfo信息
        public int DeleteBranchInfoByBranchId(int branchId)
        {
            try
            {
                int num = db.getCount("proc_deleteBranchInfobyBranchId", new SqlParameter("@branchId", branchId));
                return num;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// 查询部门信息
        /// </summary>
        /// <returns></returns>
        public IList<BranchInfo> GetBranchName()
        {
            DBHelper db = new DBHelper();
            IList<BranchInfo> list = new List<BranchInfo>();
            using (SqlDataReader reader = db.getDataReader("sp_getBranchInfo"))
            {
                while (reader.Read())
                {
                    BranchInfo bif = new BranchInfo();
                    bif.BranchName = reader["BranchName"].ToString();
                    bif.BranchId = (int)reader["branchid"];
                    list.Add(bif);
                }

            }
            db.CloseConnection();
            return list;
        }
    }
}
